23 #define D(x) cout << #x " is " << (x) << endl
25 typedef unsigned long long Int
;
26 const int BUFSIZE
= 256;
34 bool end
; //is this node a complete prefix?
35 node(bool e
) : end(e
) {
40 //not used, because I love to waste memory (It makes me feel dangerous)
42 if (n
== NULL
) return;
43 clean(n
->left
); clean(n
->right
);
47 Int
f(string s
, node
* n
){ //s = string built so far, n = the node we are standing at
48 Int left
= 0, right
= 0;
50 left
= f(s
+ "0", n
->left
);
52 if (n
->right
!= NULL
){
53 right
= f(s
+ "1", n
->right
);
56 Int x
= ((1ULL) << (M
- s
.size())) - 1;
57 if (M
- s
.size() == 64){
60 ans
[s
] = x
- left
+ 1 - right
;
66 return ret
+ left
+ right
;
71 while (scanf("%d %d\n", &n
, &M
)==2 && !(n
== 0 && M
== 0)){
73 node
* root
= new node(true);
75 fgets(buf
, BUFSIZE
, stdin
);
83 if (cur
->left
== NULL
) cur
->left
= new node(false);
85 }else if (buf
[i
] == '1'){
86 if (cur
->right
== NULL
) cur
->right
= new node(false);
97 fgets(buf
, BUFSIZE
, stdin
);
98 buf
[strlen(buf
)-2] = '\0'; //delete *\n
99 Int t
= ans
[string(buf
)];
104 fgets(buf
, BUFSIZE
, stdin
); //empty line